package com.ybb.dao;

import com.ybb.pojo.User;
import com.ybb.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class LoginDaoImpl implements LoginDao {
    public User login(String userCode, String password) {
        Connection conn = JdbcUtil.getConnection();
        String sql="select * from smbms_user where userCode=? and userPassword=?";
        PreparedStatement preparedStatement=null;
        ResultSet rs=null;
        User user=null;
        if(conn!=null){
            try {
                Object[] params={userCode,password};
                preparedStatement = conn.prepareStatement(sql);
               rs= JdbcUtil.query(preparedStatement, params);
               while(rs.next()){
                   user=new User();
                   user.setId(rs.getLong("id"));
                   user.setUserCode(rs.getString("userCode"));
                   user.setUserName(rs.getString("userName"));
                   user.setUserPassword(rs.getString("userPassword"));
                   user.setGender(rs.getInt("gender"));
                   user.setBirthday(rs.getDate("birthday"));
                   user.setPhone(rs.getString("phone"));
                   user.setUserRole(rs.getLong("userRole"));
                   user.setAddress(rs.getString("address"));
               }

            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                JdbcUtil.close(rs,preparedStatement,conn);
            }
        }

        return user;
    }
}
